package com.yhq.sensitive.util;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.time.*;
import edu.stanford.nlp.util.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
 public  class  TimeKeywordExtractor {

    public List<CoreMap> getTimeExpressions( String text) {
        // 设置属性
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, tzone");
        props.setProperty("ssplit.isOneSentence", "true");
        props.setProperty("tokenize.class", "PTBTokenizer");
        props.setProperty("pos.model", "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger");
        props.setProperty("ner.model", "edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz");
        props.setProperty("parse.model", "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
        props.setProperty("dcoref.algorithm", "greedy");
        props.setProperty("tzone.rules", "edu/stanford/nlp/models/time/rules/english-modern-distsim-date-rules");
        props.setProperty("tzone.format", "MM/dd/yyyy");
        props.setProperty("tzone", "America/New_York");
        // 创建Stanford CoreNLP对象
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        // 输入文本
        // 创建Annotation对象
        Annotation annotation = pipeline.process(text);
//        // 提取时间关键字
//        List<CoreMap> timeExpressions = TimeExpressionExtractorAnnotator.getTimestampExpressions(annotation);
//        for (CoreMap timeExpression : timeExpressions) {
//            System.out.println(timeExpression);
//        }
//        return timeExpressions;

        // 执行文本处理流水线
        pipeline.annotate(annotation);

        // 从注释中获取时间表达式列表
        List<CoreMap> timexAnnotations = annotation.get(TimeAnnotations.TimexAnnotations.class);

        // 打印提取的时间关键字
        for (CoreMap timex : timexAnnotations) {
            String temporalExpression = timex.get(CoreAnnotations.TextAnnotation.class);
            String temporalType = timex.get(TimeAnnotations.TimexAnnotation.class).timexType();
            System.out.println("时间表达式: " + temporalExpression + ", 类型: " + temporalType);
        }
    return timexAnnotations;
    }


    public static List<Integer> findSubstring(String bigString, String smallString) {
        List<Integer> positions = new ArrayList<>();
        int index = 0;
        while ((index = bigString.indexOf(smallString, index)) != -1) {
            positions.add(index);
            index += smallString.length();
        }
        return positions;
    }

}
